//define(function(require,exports){

	angular.module('shopApp')
		.controller('ListCtrl',["$rootScope","$scope","$http","$window","$routeParams","$location", function ($rootScope,$scope,$http,$window,$routeParams,$location) {

			$scope.scene = $window.localStorage.scene ? $window.localStorage.scene :"";
			//取得当前的shopId和名称
			$scope.shopId = $routeParams.cid ? $routeParams.cid :($window.localStorage.shopId?$window.localStorage.shopId:"0");
			$scope.shopName = $routeParams.cname ? $routeParams.cname : "店铺详情";
			//取得当前的菜品分类和名称
			$scope.tid = $routeParams.tid ? $routeParams.tid :"0";
			$scope.tname = $routeParams.tname ? $routeParams.tname : "店铺详情";

			//头部导航
			$rootScope.pagetitle= $scope.tname ? $scope.tname : ($scope.shopName? $scope.shopName: "店铺详情");
			if($scope.tid){
				//所有分类的菜
				$rootScope.back = '<a href="#/list?cid=0"><i class="icon-font" title="返回">&#xe61f;</i></a>';
			}else{
				$rootScope.back = '<a href="#/about?cid='+$scope.shopId+'&cname='+$scope.shopName+'"><i class="icon-font" title="返回">&#xe61f;</i></a>';
			}
			$rootScope.next = '<a href="/res/static/index.html#/auth" target="_self"><i class="icon-font" title="我的">&#xe61e;</i></a>';

			//iscroll
			var myScroll;
			$scope.scroll = function(){
				myScroll = new IScroll('#js_scroll', {
					scrollX: true,
					scrollY: false,
					mouseWheel: true,
					scrollbars: false,
					interactiveScrollbars: true,
					shrinkScrollbars: 'scale',
					fadeScrollbars: true
				})
			}
			//所有菜品分类
			$scope.hasCate = false;
			$http.get(hpfbase.url+"/shop/dish-type-list.jhtm",{
				shopId:$window.localStorage.shopId
			})
				.success(function(ret){
					if(ret.statusCode)
					$scope.categorys = ret.data;
					if ($scope.categorys) {
						$scope.hasCate = true;
					};
				});

			//当前分类下的菜品
			$scope.dishList = [];
			$scope.pageNum =1;
			$http.get(hpfbase.url+"/shop/dish-list.jhtm",{
				dishtype:$scope.tid ,
				shopId:$window.localStorage.shopId,
				pageNum:$scope.pageNum,
				numPerPage:20
			})
			.success(function(ret){
				if(ret.statusCode){
					$scope.dishList = ret.data.dataList;
					if ($scope.dishList) {
						$scope.hasDish = true;
					};
				}
			});

            //加载分页内容
            $scope.showMore = function() {
				$scope.pageNum = $scope.pageNum++;
				$http.get(hpfbase.url+"/shop/dish-list.jhtm",{
					dishtype:$scope.tid ,
					shopId:$window.localStorage.shopId,
					pageNum:$scope.pageNum,
					numPerPage:20
				})
					.success(function(ret){
						if($scope.pageNum>=ret.totalPage){
							return false;
						}
						if(ret.statusCode){
							//追加数据
                            for(var i=0; i<ret.data.dataList.length;i++){
								$scope.dishList.push(ret.data.dataList[i]);
							}
						}
					});
            };

		//查看详情
		$scope.viewItem = function(obj){
			$scope.item = obj;
			$scope.itemDialog = true;
		}

		//购物车
		$scope.cart =$window.localStorage.cart ? JSON.parse($window.localStorage.cart) : [];
		//找到当前索引
		$scope.findIndex = function(id) {
			var index;
			angular.forEach($scope.cart, function (item, key) {
				if (item.id === id) {
					index = key;
					return;
				}
			})
			return index;
		}
		/**
		 * 购物车
		 **/
		$scope.qty = $window.localStorage.qty ? parseInt($window.localStorage.qty) : 0;
		$scope.total = $window.localStorage.total ? parseFloat($window.localStorage.total) : 0;
		$scope.vipTotal =  $window.localStorage.vipTotal ? parseFloat($window.localStorage.vipTotal) : 0;
		$scope.tocalAmount =$window.localStorage.tocalAmount ? parseFloat($window.localStorage.tocalAmount):0;
		$scope.addtoCart= function(id,dishName,price,vipPrice,amount,type){
			if($scope.cart.length>0 && type){
				var index = $scope.findIndex(id);
				var quantity = parseInt($scope.cart[index].quantity);
				if(index !== -1){
					if(type=="add"){
						$scope.cart[index].quantity=quantity+1;
						$scope.qty += 1;
						$scope.total += parseFloat(price);
						$scope.vipTotal +=  parseFloat(vipPrice);
						$scope.tocalAmount += parseFloat(amount);
					}else if(type=="reduce"){
						if(quantity>1){
							$scope.cart[index].quantity=quantity-1;
						}else{
							$scope.cart.splice(index,1);
						}
						$scope.qty -= 1;
						$scope.total -= parseFloat(price);
						$scope.vipTotal -=  parseFloat(vipPrice);
						$scope.tocalAmount -=parseFloat(amount);
					}
				}
			}else{
				$scope.cart.push(
					{
						"id":id,
						"dishName":dishName,
						"quantity":1,
						"price":parseFloat(price),
						"vip":parseFloat(vipPrice),
						"amount":parseFloat(amount)
					}
				);
				$scope.qty += 1;
				$scope.total += parseFloat(price);
				$scope.vipTotal +=  parseFloat(vipPrice);
				$scope.amount =parseFloat(amount);
				$scope.tocalAmount+=parseFloat(amount);
			}
			$window.localStorage.cart=JSON.stringify($scope.cart);
			$window.localStorage.qty = $scope.qty;
			$window.localStorage.total = $scope.total =parseFloat($scope.total.toFixed(2));
			$window.localStorage.vipTotal = $scope.vipTotal = parseFloat($scope.vipTotal.toFixed(2));
			$window.localStorage.tocalAmount = $scope.tocalAmount;
			//console.log($scope.cart);
		}

		/**
		* 去结账
	    * 如果是扫码进来则直接提交订单
		**/
		$scope.order = function(){
			if($scope.scene =="dish"){
				 var data= "shopId="+$scope.shopId+
					 "tableId="+$scope.tableId+
					 "detail="+JSON.stringify($scope.cart);
                 $http.post(hpfbase.url+"/order/createOrder.jhtm",
					 data,
					 {headers: {
						 'Content-Type': "application/x-www-form-urlencoded;charset=utf-8"
					 }}
				 )
				 .success(function(ret){
					 $location.path("/sucess");
				 });
			}else{
				$scope.url = {
					link:hpfbase.url+"/res/static/index.html#/login"
				};
			    //验证是否登录
				$http.get(hpfbase.url+"/user/isLogin.jhtm")
					.success(function(ret){
						if(ret.succeed){
							$scope.url = {
								link:hpfbase.url+"/res/static/shop.html#/pay"
							};
						}
					});
			}
		};

	}]);
//});
